// packageB/member/group/ywmGroupsMyGroups/ywmGroupsMyGroups.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeName: 0,
    dataList: [],
    titleArr: [],

    cardTotal: '---',//开团卡总数量
    isLoadMore: true, //判断是否要加载更多的标志

    showActivePop: false,//点击去开团，展示拼团列表
    groupList: [],
    selectGroupId: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initOptions();
  },

  initOptions() {
    let GROUPlIST_URL = 'plugin.ywm-fight-groups.frontend.controllers.fight-groups.get-member-join-teams';//拼团列表，1-正在拼团，2-拼团成功，3-拼团失败
    let OPENCARD_URL = 'plugin.ywm-fight-groups.frontend.controllers.fight-groups.my-open-group-card';//我的开团卡
    this.data.dataList = [];
    this.data.titleArr = [{
      name: '正在拼团',
      url: GROUPlIST_URL,
      param: {
        team_status: 1
      }
    }, {
      name: '拼团成功',
      url: GROUPlIST_URL,
      param: {
        team_status: 2
      }
    }, {
      name: '拼团失败',
      url: GROUPlIST_URL,
      param: {
        team_status: 3
      }
    }, {
      name: '我的开团卡',
      url: OPENCARD_URL,
      param: {},
      callback: (data) => {
        this.data.isLoadMore = true;
        this.data.dataList[this.data.activeName].isLoadMore = true;
        this.data.dataList[this.data.activeName].total_page = data.data.last_page;
        this.data.dataList[this.data.activeName].page = data.data.current_page;
        this.setData({
          cardTotal: data.card_number,
          ['dataList[' + this.data.activeName + '].list']: this.data.dataList[this.data.activeName].list.concat(data.data.data)
        })
      }
    }];
    this.setData({
      titleArr: this.data.titleArr
    })
    this.initData();
  },
  initData() {
    this.data.dataList = [];
    this.data.isLoadMore = true;
    this.data.titleArr.forEach((val, index) => {
      this.data.dataList.push({
        isLoadMore: true,
        page: 1,
        total_page: 0,
        list: []
      });
    });
    this.getData();
  },
  onChange(event) {
    this.setData({
      activeName: event.detail.index
    });
    this.getData();
  },

  getData(){
    if (this.data.dataList[this.data.activeName].list.length > 0) {
      return;
    }

    let json = {...this.data.titleArr[this.data.activeName].param};
    let _url = this.data.titleArr[this.data.activeName].url;

    app._getNetWork({
      url: app.getNetAddresss(_url),
      data: json,
      success: (resdata) => {
        var response = resdata.data;
        if (response.result == 1) {
          if (this.data.titleArr[this.data.activeName].callback) {
            return this.data.titleArr[this.data.activeName].callback(response.data);
          }
          this.data.isLoadMore = true;
          this.data.dataList[this.data.activeName].isLoadMore = true;
          this.data.dataList[this.data.activeName].total_page = response.data.last_page;
          this.data.dataList[this.data.activeName].page = 1;
          this.setData({
            ['dataList[' + this.data.activeName + '].list']: response.data.data
          })
        } else {
          app.tips(res.msg)
        }
      }
    });
  },
  clickBtn(e) {
    // 按钮值：1-邀请好友,2-订单详情,3-拼团详情,4-去开团,5-使用开团卡开团
    const { value, id, orderid, good,tid } = e.target.dataset;
    let key = Number(value);
    switch (key) {
      case 2:
        if(orderid) {
          this.gotoOrder(orderid);
        }
        break;
      case 3:
        this.gotoGroup(tid);
        break;
      case 4:
        this.data.selectGroupId = id;
        this.getActiveList();
        break;
      case 5:
        this.gotoGroupList(id)
        break;
      default:
        break;
    }
  },
  gotoGroupList(id){
    wx.navigateTo({
      url: '/packageB/member/group/GroupList/GroupList?id=' + id + '&types=ywmGroup'
    });
  },
  gotoOrder(order_id) {
    wx.navigateTo({
      url: '/packageA/member/orderdetail/orderdetail?order_id=' + order_id + '&orderType=ywmgroups'
    });
  },
  gotoGroup(id) {
    wx.navigateTo({
      url: '/packageB/member/group/GroupDetail/GroupDetail?id=' + id + '&plu=ywm'
    });
  },
  getActiveList(){
    app._getNetWork({
      url: app.getNetAddresss('plugin.ywm-fight-groups.frontend.controllers.fight-groups.index'),
      data: {},
      success: (resdata) => {
        var response = resdata.data;
        if (response.result == 1) {
          this.setData({
            groupList: response.data.data,
            showActivePop: true
          })
        } else {
          app.tips(res.msg)
        }
      }
    });
  },
  openTeam(e){
    const {id, title} = e.detail;
    let that = this;
    wx.showModal({
      title: '提示',
      content: `是否确认选中【${title}】开团`,
      success (res) {
        if (res.confirm) {
          that.openTeamQuest(id);
        }
      }
    })
  },
  openTeamQuest(id){
    app._getNetWork({
      url: app.getNetAddresss('plugin.ywm-fight-groups.frontend.controllers.fight-groups.auto-open-group'),
      data: {
        team_member_id: this.data.selectGroupId,
        group_id: id
      },
      success: (resdata) => {
        var response = resdata.data;
        app.tips(response.msg)
        if (response.result == 1) {
          this.setData({
            showActivePop: false
          })
          this.initData();
        }
      }
    });
  },
  hiddenActivePop(){
    this.setData({
      showActivePop: false
    })
  },
  onClickRight(){
    wx.navigateTo({url: '/packageB/member/group/ywmGroupsDetailed/ywmGroupsDetailed'})
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.dataList[this.data.activeName].isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },
  _getMoreData() {
    this.data.dataList[this.data.activeName].isLoadMore = false;
    if (this.data.dataList[this.data.activeName].page >= this.data.dataList[this.data.activeName].total_page) {
      return;
    } else {
      let json = {...this.data.titleArr[this.data.activeName].param};
      let _url = this.data.titleArr[this.data.activeName].url;
      let page = this.data.dataList[this.data.activeName].page + 1
      let urlStr = app.getNetAddresss(_url);
      urlStr = urlStr + '&page=' + page;
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata)=> {
          var res = resdata.data;
          if (res.result == 1) {
            let myData = res.data.data;
            if (this.data.titleArr[this.data.activeName].callback) {
              return this.data.titleArr[this.data.activeName].callback(res.data);
            }
            this.data.dataList[this.data.activeName].isLoadMore = true;
            this.data.dataList[this.data.activeName].page = page;
            this.setData({
              ['dataList[' + this.data.activeName + '].list']: this.data.dataList[this.data.activeName].list.concat(myData)
            });
          } else {
            this.data.dataList[this.data.activeName].isLoadMore = false;
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    var value = wx.getStorageSync('yz_uid');
    var mid = '';
    if (value) {
      mid = value;
    }
    if(e && e.from == 'button'){
      const {tid , good} = e.target.dataset
      let _path = '/packageB/member/group/GroupDetail/GroupDetail?id=' + tid + '&plu=ywm' + '&mid=' + mid;
      return {
        title: good.title,
        path: _path
      }
    }
  }
})